﻿using System.Web;
using System.Web.SessionState;

namespace RazakiSite.PagesPublic
{
    /// <summary>
    /// Summary description for FileWrite
    /// </summary>
    public class FileWrite : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string type = UtilitiesUI.GetQueryString<string>("type");
            string name = UtilitiesUI.GetQueryString<string>("name");

            SetResponse(context, type, name);

            UtilitiesUI.SessionClear(name);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void SetResponse(HttpContext context, string type, string name)
        {
            string contentType = "text/plain";
            byte[] file = UtilitiesUI.SessionGet<byte[]>(name);

            switch (type)
            {
                case "image":
                    contentType = "image/jpeg";
                    break;
            }

            context.Response.ContentType = contentType;
            context.Response.BinaryWrite(file);
        }

    }
}